#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cerrno>

const uint16_t port =8080;

int main()
{
    int sock=socket(AF_INET,SOCK_DGRAM,0);
    
    if(sock<0)
    {
        std::cerr<<"socket creat error"<<errno<<std::endl;
        return 1;
    }

    struct sockaddr_in local;
    local.sin_family=AF_INET;
    local.sin_port=htons(port);
   // local.sin_addr.s_addr=inet_addr("`");
    local.sin_addr.s_addr=INADDR_ANY;

    if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
    {
          std::cerr<<"socket bind error"<<errno<<std::endl;
          return 2;
    }

    bool quit=false;
    #define NUM 1024
    char buf[NUM];
    while(!quit)
    {
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer);
        
        recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&peer,&len);
        std::cout<<"client say: "<<buf<<std::endl;
        std::string str="hello";
        sendto(sock,str.c_str(),str.size(),0,(struct sockaddr*)&peer,len);

    }


    

    return 0;
}